#include "message_receiver.hpp"

#include <boost/foreach.hpp>

#include "client.hpp"

message_receiver::message_receiver()
{
}

std::stringstream& message_receiver::write_var_string(std::stringstream& stream, const std::string& s)
{
    stream << (unsigned char)s.size() << s;
    return stream;
}

std::stringstream& message_receiver::write_var_strings(std::stringstream& stream, const std::vector<std::string>& strings)
{
    BOOST_FOREACH(std::string s, strings)
    {
        message_receiver::write_var_string(stream, s);
    }
    return stream;
}

std::string message_receiver::read_var_string(const std::string& s, const int index)
{
    return s.substr(index + 1, s[index]);
}

std::vector<std::string> message_receiver::read_var_strings(const std::string& s, const int index)
{
    std::vector<std::string> ret;
        unsigned int i = index;
    while(i < s.size())
    {
        std::string stringName = message_receiver::read_var_string(s, i);
        ret.push_back(stringName);
        i += stringName.size() + 1;
    }
    return ret;
}
